package be.kul.osgi.ds.myagent.Role;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.xml.sax.SAXException;

import be.kul.osgi.ds.irole.IRole;
import be.kul.osgi.ds.irole.ITask;
import be.kul.osgi.ds.myagent.MyAgent;
import be.kul.osgi.ds.myagent.message.ContractNetInitiator;

public class TaskHandler {

	MyAgent agent;
	
	
	public TaskHandler(MyAgent agent){
		this.agent = agent;
	}
	
	public ArrayList<ITask> getTasks(IRole r){
		return r.getTasks();
	}
	
	
	/*public void shareAllTasks(IRole r){
		ArrayList<ITask> tasks = r.getTasks();
		Iterator<ITask> i = tasks.iterator();
		while (i.hasNext()){
			this.shareTask(i.next(), r);
		}
		
	}*/
	
	public ContractNetInitiator shareTask(ITask t, IRole role){
		String s = "<Task>" +this.agent.XMLStreamservice.toXML(t) + "</Task>";
		String c = "";
		//time-out is 5000ms
		//
		//get dependencies
		RoleHandler r = new RoleHandler(role, this.agent);
		String deps = r.getDependenciesXML();
		
		
		c = deps;
		//if task is activetask
		//System.out.println("Agent's current tack : " + agent.schedule.current.task.getID());
		//System.out.println("ID in simulator : " + agent.thread.getCurrentTaskID());
		
		if (t.getID().equals(agent.thread.getCurrentTaskID())){
			String state = role.getCurrentTaskState(agent.name);
			if(state != null && !state.equals("")) {
				s = s + "<CState>" + state + "</CState>";
				//System.out.println(s);
			}
		}
		//than add State
		
		ContractNetInitiator ci = new ContractNetInitiator(agent, s, c);
		
		return ci;		
	}
	
	
	public IRole getRoleFromTask(String xml) {
		try {
			String x = agent.XMLStreamservice.getRoot(xml);
			//System.out.println(x);
			Collection<ServiceReference<IRole>> roles = agent.context.getServiceReferences(IRole.class, null);
			Iterator<ServiceReference<IRole>> i = roles.iterator();
			while(i.hasNext()){
				ServiceReference<IRole> sr = i.next();
				if(sr.getBundle().getSymbolicName().toString().equals(x)){
					//System.out.println("Bingo");
					return (IRole)agent.context.getService(sr);
				}
			}
			
		} catch (InvalidSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		return null;
	}
	
	public IRole getRoleFromTask(ITask t){
		//return t.getClass().getPackage().toString();
		String pack = t.getClass().getPackage().getName();
		//System.out.println(pack);
		try {
			Collection<ServiceReference<IRole>> roles = agent.context.getServiceReferences(IRole.class, null);
			Iterator<ServiceReference<IRole>> i = roles.iterator();
			while(i.hasNext()){
				ServiceReference<IRole> sr = i.next();
				
				if(sr.getBundle().getSymbolicName().toString().equals(pack)){
					//System.out.println("Bingo");
					return (IRole)agent.context.getService(sr);
				}
			}
			
		} catch (InvalidSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
}
